const { Vector } = require("../vector")

const PersonWidth = 20  //人的宽度和高度
const PersonHeight = 80
const GenerationSpace = PersonHeight * 0.6 //代与代之间的垂直距离
const CoupleLineHeight = GenerationSpace * 0.3  //夫妻连接线的高度
const BrotherLineHeight = GenerationSpace * 0.3 //兄弟连接线的高度
const CouplePadding = PersonWidth * 0.6  //夫妻之间的间隔距离
const TreeSpacing = PersonWidth * 0.8  //人与人之间的最小距离（优先级小于CouplePadding）

//单个人
class Person {
  //构造函数，pos是一个Vector(上面中点位置), name是人名, id是后端的person的ID
  constructor(tree_drawer, pos, name, id) {
    this.tree_drawer = tree_drawer
    this.pos = pos.Copy()
    this.name = name
    if (this.name.length == 0) {
      this.name = "无名"
    }
    this.id = id
    this.is_select = false
    this.is_tap = false  //是否按下
  }

  //绘制函数
  Draw() {
    //绘制边框
    let cache = this.tree_drawer.cache
    let dest = cache.ConvertPosition(this.pos)  
    let w = cache.ConvertDistance(PersonWidth)
    let h = cache.ConvertDistance(PersonHeight)
    dest.Add(new Vector(0, h/2))  //dest是框的中心位置
    let drawer = this.tree_drawer.drawer
    if (!this.is_select) {
      drawer.DrawRectByCenter(dest.x, dest.y, w, h)
    } else {
      drawer.DrawRectByCenterWithColor(dest.x, dest.y, w, h, "#FF6600", "#FFFFCC")
    }

    if (this.is_tap) {
      drawer.FillRectByCenter(dest.x, dest.y, w, h, "#FFFF66")
    }

    //绘制文字
    if (this.name.length == 1) {
      let font_size = cache.ConvertDistance(16)
      let adjust = cache.ConvertDistance(3)
      drawer.DrawSingleWord(this.name, dest.x, dest.y - adjust, font_size)

    } else if (this.name.length == 2) {
      let font_size = cache.ConvertDistance(16)
      let adjust = cache.ConvertDistance(3)
      drawer.DrawSingleWord(this.name[0], dest.x, dest.y - h/4 - adjust, font_size)
      drawer.DrawSingleWord(this.name[1], dest.x, dest.y + h/4 - adjust, font_size)

    } else if (this.name.length == 3) {
      let font_size = cache.ConvertDistance(16)
      let adjust = cache.ConvertDistance(3)
      drawer.DrawSingleWord(this.name[0], dest.x, dest.y - h/4 - adjust, font_size)
      drawer.DrawSingleWord(this.name[1], dest.x, dest.y - adjust, font_size)
      drawer.DrawSingleWord(this.name[2], dest.x, dest.y + h/4 - adjust, font_size)

    } else if (this.name.length == 4) {
      let font_size = cache.ConvertDistance(16)
      let adjust = cache.ConvertDistance(3)
      drawer.DrawSingleWord(this.name[0], dest.x, dest.y - h*3/8 - adjust, font_size)
      drawer.DrawSingleWord(this.name[1], dest.x, dest.y - h/8 - adjust, font_size)
      drawer.DrawSingleWord(this.name[2], dest.x, dest.y + h/8 - adjust, font_size)
      drawer.DrawSingleWord(this.name[3], dest.x, dest.y + h*3/8 - adjust, font_size)

    } else if (this.name.length == 5) {
      let font_size = cache.ConvertDistance(14)
      let adjust = cache.ConvertDistance(2)
      drawer.DrawSingleWord(this.name[0], dest.x, dest.y - h*0.4 - adjust, font_size)
      drawer.DrawSingleWord(this.name[1], dest.x, dest.y - h*0.2 - adjust, font_size)
      drawer.DrawSingleWord(this.name[2], dest.x, dest.y - adjust, font_size)
      drawer.DrawSingleWord(this.name[3], dest.x, dest.y + h*0.2 - adjust, font_size)
      drawer.DrawSingleWord(this.name[4], dest.x, dest.y + h*0.4 - adjust, font_size)

    } else if (this.name.length > 5) {
      let font_size = cache.ConvertDistance(14)
      let adjust = cache.ConvertDistance(2)
      drawer.DrawSingleWord(this.name[0], dest.x, dest.y - h*0.4 - adjust, font_size)
      drawer.DrawSingleWord(this.name[1], dest.x, dest.y - h*0.2 - adjust, font_size)
      drawer.DrawSingleWord(this.name[2], dest.x, dest.y - adjust, font_size)
      drawer.DrawSingleWord(this.name[3], dest.x, dest.y + h*0.2 - adjust, font_size)
      drawer.DrawSolidCircle(dest.x, dest.y + h*0.35 - cache.ConvertDistance(1), cache.ConvertDistance(1))
      drawer.DrawSolidCircle(dest.x, dest.y + h*0.4 - cache.ConvertDistance(1), cache.ConvertDistance(1))
      drawer.DrawSolidCircle(dest.x, dest.y + h*0.45 - cache.ConvertDistance(1), cache.ConvertDistance(1))

    }
   
  }

  //获取固定大小
  GetFixedSize() {
    return new Vector(PersonWidth, PersonHeight)
  }

  //移动
  Move(vec) {
    this.pos.Add(vec)
  }

  //返回某个位置是否在范围内， pos是一个vector对象
  IsInRange(pos) {
    let diff = pos.Copy().Sub(this.pos)
    return Math.abs(diff.x) <= PersonWidth / 2 && diff.y >= 0 && diff.y <= PersonHeight
  }

  //设置选中状态
  SetSelect(bSelect) {
    this.is_select = bSelect
  }

  //设置按下状态
  SetTap(bTap) {
    this.is_tap = bTap
  }
}

module.exports = {
  Person,
  PersonWidth,
  PersonHeight,
  GenerationSpace,
  CoupleLineHeight,
  BrotherLineHeight,
  CouplePadding,
  TreeSpacing
}